package com.chessbit.snippet

import scala.xml.{NodeSeq,Text}
import net.liftweb.http.{FileParamHolder,S,SHtml,StatefulSnippet}
import net.liftweb.common.{Box,Empty,Full}
import net.liftweb.util.Helpers.{bind,chooseTemplate}
import net.liftweb.util.Helpers._
import net.liftweb.mapper._
import java.io.File
import java.io.FileOutputStream;
import _root_.com.chessbit.model._
import java.io._
import scala.io._
import com.file.Control._
import com.chess.parse.PGNParser
import com.chess.parse._

class FileUpload {
        var fileHolder : Box[FileParamHolder] = Empty
                
        def upload(xhtml : NodeSeq) : NodeSeq = {
                def doUpload() = {
                        fileHolder match {
                        // An empty upload gets reported with a null mime type,
                        // so we need to handle this special case
                        case Full(FileParamHolder(_, null, _, _)) => S.error("empty receipt attachment")
                        case Full(FileParamHolder(_, mime, name, data)) => {
                        	using(reader(data)) { _ match {
                        	  case Some(buffer) => {
		                              PGNParser.parse(buffer).get match {
		                                case g::tail => {
		                                  val game = Game.createRecord
		                                  .title(g.getTagValue(TagName.Event))
		                                  .result(g.getTagValue(TagName.Result))
		                                  .site(g.getTagValue(TagName.Site))
		                                  .round(g.getTagValue(TagName.Round))
		                                  .date(g.getTagValue(TagName.Date))
		                                  .white(g.getTagValue(TagName.White))
		                                  .black(g.getTagValue(TagName.Black))
		                                  .blackElo(g.getTagValue(TagName.BlackElo))
		                                  .whiteElo(g.getTagValue(TagName.WhiteElo))
		                                  .plyCount(g.moves.size)
		                                  .ECO(g.getTagValue(TagName.ECO))
		                                  .moves(g.moves.map( mv => Move(mv.move.value, mv.comment)))
		                                  .save
		                                  S.redirectTo("/view?id="+game.id.is)
		                                }
		                                case _ =>  S.error("Sorry. We could not parse your file. Is it a PGN file?")
		                              }
                        	  }
                        	  case None => case _ => S.error("Sorry. We can't read your file")
                        	}
                        	}
                        }
                        case Full(_) => S.error("Invalid receipt attachment")
                        case _ => S.error("Empty receipt attachment")
                        }
                }
                bind("entry", xhtml,"filename" -> SHtml.fileUpload(fph => fileHolder = Full(fph)),"submit" -> SHtml.submit("Upload", doUpload) ) 
        
        }
}